<?php

declare(strict_types=1);

namespace app\admin\model;


/**
 * @mixin \think\Model
 */
class ProductMerge extends BaseModel
{
    //
    protected $table = 'db_product_merge';

    // 定义允许更新的字段
    protected $updateFields = [
        'productname',
        'producturlname',
        'primary_pid',
        'sales_pids',
        'estimate_price',
        'product_photo',
        'closeshots_photo',
        'pmv_id',
        'smv_id',
        'describe',
        'sort',
        'full_describe',
        'qanda_describe',
        'seotitle',
        'seodesc',
        'status'
    ];

    public function getUpdateFields()
    {
        return $this->updateFields;
    }

    protected $autoWriteTimestamp = true;

    protected $createTime = 'create_time';

    protected $updateTime = 'update_time';

    public $attributes = [
        'storename' => [
            1 => 'AliExpress',
            2 => 'Ebay',
            3 => 'Yandex Market',
            4 => 'Amazon',
            5 => 'Ozon',
            6 => 'Shopee',
            7 => 'LAZADA',
            8 => 'Taobao',
        ]
    ];

    /**
     * @describe 获取枚举值
     * @author: Bobo
     * @date: 2025-04-03 11:22:52 
     * @param [type] @required
     * @return Renderable
     */
    public function getFieldEnum($field = 'all')
    {
        return $this->getEnum(strtoupper($field), [], 'label');
    }

    public function storename()
    {
        return $this->hasMany(ProductMergeBuylink::class, 'pm_id', 'id');
    }

    public function primaryPid()
    {
        return $this->belongsTo(Product::class, 'primary_pid', 'id')
            ->bind([
                'primary_pid_name' => 'cname'
            ]);
    }

    public function productRelation()
    {
        return $this->hasMany(ProductRelation::class, 'pm_id', 'id')
            ->with(['product' => function ($query) {
                $query->field(['id', 'cname'])
                    ->bind(['cname' => 'cname']);
            }])
            ->where('db_product_relation.is_del', 0);  // 添加软删除条件
    }


    public function pmvId()
    {
        return $this->belongsTo(MinerVideos::class, 'pmv_id', 'id')
            ->bind([
                'pmv_id_name' => 'video_title'
            ]);
    }
}
